package archer.application.core.entity.system;

import archer.application.core.constant.Constant;
import archer.framework.common.entity.BaseEntity;
import archer.framework.utils.ValidateUtils;
import jodd.json.meta.JSON;
import jodd.typeconverter.Convert;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.UniqueConstraint;
import java.sql.Timestamp;

/**
 * @author Dendi
 * @date 2016/7/3
 */
@Entity
@UniqueConstraint(columnNames = {"type", "code"})
public class Rule extends BaseEntity {
    public static final Find<Rule> find = new Find<Rule>(Rule.class);

    @Column(columnDefinition = " varchar(64) BINARY COMMENT '规则类型'")
    private String type;

    @Column(columnDefinition = " varchar(64) BINARY COMMENT '规则编码'")
    private String code;

    @Column(columnDefinition = " varchar(64) COMMENT '规则'")
    private String val;

    @Column(columnDefinition = " varchar(32) default 'STRING' COMMENT '值类型'")
    private String valueType = Constant.ValueType.STRING;

    @Column(columnDefinition = " varchar(12) default 'STRING' COMMENT '规则状态'")
    private String status = Constant.RuleStatus.NEW;

    @Column(columnDefinition = " varchar(256) COMMENT '规则说明'")
    private String remark;

    public String getType() {
        return type;
    }

    public Rule setType(String type) {
        this.type = type;
        return this;
    }

    public String getCode() {
        return code;
    }

    public Rule setCode(String code) {
        this.code = code;
        return this;
    }

    public String getVal() {
        return val;
    }

    public Rule setVal(String val) {
        this.val = val;
        return this;
    }

    public String getValueType() {
        return valueType;
    }

    public Rule setValueType(String valueType) {
        this.valueType = valueType;
        return this;
    }

    public String getStatus() {
        return status;
    }

    public Rule setStatus(String status) {
        this.status = status;
        return this;
    }

    public String getRemark() {
        return remark;
    }

    public Rule setRemark(String remark) {
        this.remark = remark;
        return this;
    }

    /**
     * 获取规则配置
     *
     * @return
     */
    @JSON(include = false)
    public <T> T getValue() {

        if (ValidateUtils.isNotEmpty(this.val)) {
            if (Constant.ValueType.STRING.equals(this.valueType)) {
                return (T) Convert.toString(this.val);
            }
            if (Constant.ValueType.TIMESTAMP.equals(this.valueType)) {
                return (T) (new Timestamp(Convert.toDate(this.val).getTime()));
            }
            if (Constant.ValueType.BIG_DECIMAL.equals(this.valueType)) {
                return (T) Convert.toBigDecimal(this.val);
            }
            if (Constant.ValueType.DATE.equals(this.valueType)) {
                return (T) Convert.toDate(this.val);
            }
            if (Constant.ValueType.INTEGER.equals(this.valueType)) {
                return (T) Convert.toInteger(this.val);
            }
        }

        return (T) this.val;
    }

}
